import 'package:flutter/material.dart';
import 'package:gameplay_flutter/m_room/model/Room_mic_model.dart';
import 'package:gameplay_flutter/m_room/model/room_base_model.dart';
import 'package:get/get.dart';

import '../../lib_network/http_helper.dart';
import '../model/room_data_model.dart';
import '../room_chat/model/room_message_event.dart';
import '../room_api.dart';

class RoomController extends GetxController {
  RoomDataModel? roomDataModel;

  var mics = <RoomMicModel>[].obs;
  var isInputVisible = false.obs;

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void handleMessage(RoomMessageEvent event) {


  }

  /// 获取房间信息
  void extraInfo() async {
    var res = await HttpHelper.instance().post(
      RoomApi.extraInfo,
      body: {
        "roomNo": roomDataModel?.roomInfo?.roomNo ?? "",
        "riskPoint": true,
      },
      showLoading: true,
      showToast: true,
    );
    if (res.result is Map<String, dynamic>) {}
  }

  ///  获取麦位信息
  void micAll() async {
    var res = await HttpHelper.instance().post(
      RoomApi.micAll,
      body: {
        "roomNo": roomDataModel?.roomInfo?.roomNo ?? "",
      },
      showLoading: true,
      showToast: true,
    );
    if (res.isSuccess &&
        res.result is Map<String, dynamic> &&
        res.result["roomMicList"] is List) {
      mics.clear();
      res.result["roomMicList"].forEach((element) {
        mics.add(RoomMicModel.fromJson(element));
      });
    }
  }

  /// 获取房间消息
  void messageSingleRoom() async {
    var res = await HttpHelper.instance().post(
      RoomApi.messageSingleRoom,
      body: {
        "roomNo": roomDataModel?.roomInfo?.roomNo ?? "",
      },
      showLoading: true,
      showToast: true,
    );
    if (res.result is Map<String, dynamic>) {

    }
  }

  /// 上麦
  void holdUp(String userId, int index) async {
    var res = await HttpHelper.instance().post(
      RoomApi.holdUp,
      body: {
        "roomNo": roomDataModel?.roomInfo?.roomNo ?? "",
        "userId": userId,
        "index": index,
      },
      showLoading: true,
      showToast: true,
    );
    if (res.isSuccess &&
        res.result is Map<String, dynamic> &&
        res.result["roomMicInfos"] is List) {
      mics.clear();
      res.result["roomMicInfos"].forEach((element) {
        mics.add(RoomMicModel.fromJson(element));
      });

    }else {

    }
  }

  /// 设置麦位状态
  void holdStatus(String userId, int index) async {
    var res = await HttpHelper.instance().post(
      RoomApi.holdUp,
      body: {
        "roomNo": roomDataModel?.roomInfo?.roomNo ?? "",
        "userId": userId,
        "index": index,
      },
      showLoading: true,
      showToast: true,
    );
    if (res.isSuccess &&
        res.result is Map<String, dynamic> &&
        res.result["roomMicInfos"] is List) {
      mics.clear();
      res.result["roomMicInfos"].forEach((element) {
        mics.add(RoomMicModel.fromJson(element));
      });

    }else {

    }
  }
}
